package com.firepongo.chaos.web.service.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j

public class InitWebCheckService {
    @Value("${app.admin.host:http://127.0.0.1:38089}")
    private String adminHost;
    @Value("${app.admin.iflog:false}")
    private boolean iflog;
    @Value("${app.admin.username:}")
    private String username;
    @Value("${app.admin.password:}")
    private String password;
    @Value("${app.sign:chaos}")
    private String appSign;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${app.swagger.name:阿暴}")
    private String swaggerName;
    @Value("${app.swagger.url:https://gitee.com/ape-stack/chaos}")
    private String swaggerUrl;
    @Value("${app.swagger.email:632381896@QQ.com}")
    private String swaggerEmail;


    @Value("${app.baidu.lbsyun.ak:}")
    private String baiduLbsyunAk;
    @Value("${app.baidu.lbsyun.sk:}")
    private String baiduLbsyunSk;

    @Value("${app.amap.key:}")
    private String amapKey;

    @Value("${app.wxmini.appid:}")
    private String wxminiAppid;
    @Value("${app.wxmini.secret:}")
    private String wxminiSecret;

    public void check() {
        log.info("【Chaos】:Web开始参数检查！");
        adminNoCheck();
        log(iflog, "已开启日志记录！", "未开启日志记录！");
        log("chaos".equals(appSign), "manage(Jwt)使用默认配置", "manage(Jwt)已配置");
        log(!swaggerName.equals("阿暴") && !swaggerUrl.equals("https://gitee.com/ape-stack/chaos") && !swaggerEmail.equals("632381896@QQ.com"), "Swagger联系人已启用", "Swagger使用默认联系人【阿暴】【632381896@QQ.com】");
        log(!StringUtils.isEmpty(baiduLbsyunAk) && !StringUtils.isEmpty(baiduLbsyunSk), "百度LBS服务已启用", "百度LBS服务未启用");
        log(!StringUtils.isEmpty(amapKey), "高德位置服务已启用", "高德位置服务未启用");
        log(!StringUtils.isEmpty(wxminiAppid) && !StringUtils.isEmpty(wxminiSecret), "微信小程序服务已启用", "微信小程序服务未启用");

        log.info("【Chaos】:Web完成参数检查！");
    }

    private void adminNoCheck() {
        if (!isAdminService()) {
            log(!"http://127.0.0.1:38089".equals(adminHost), "admin系统使用:" + adminHost, "使用local admin系统");
            log(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password), "已登录admin系统平台", "admin系统用户名/密码配置缺失!");
        }
    }

    private void log(boolean is, String yesLog, String noLog) {
        if (is) {
            log.info("【Chaos】:{}", yesLog);
        } else {
            log.warn("【Chaos】:{}", noLog);
        }
    }

    public boolean isAdminService() {
        return applicationName.equals("chaos-admin-service");
    }
}
